// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"


#include "MainFrm.h"
#include "AxialView.h"
#include "CoronalView.h"
#include "SagittalView.h"
#include "MipView.h"
#include "PolygonLeaf.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

Raw3DViewer g_raw3DViewer;
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()

END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};


// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	bSplitter = false;
}

CMainFrame::~CMainFrame()
{
	delete m_pDialogMain;
}


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	CRect rect;
	this->GetClientRect(&rect);

	m_wndReBar.Create(this,RBS_BANDBORDERS,WS_CHILD | WS_VISIBLE 
					  | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_RIGHT);  

	m_pDialogMain = new CDialogMain;
	m_pDialogMain->Create(IDD_DIALOGMAIN,this);   

	REBARBANDINFO   rbi;   
	rbi.cbSize		=   sizeof(REBARBANDINFO);   
	rbi.fMask		=	RBBIM_CHILD | RBBIM_CHILDSIZE | RBBIM_STYLE | RBBIM_TEXT   ;   
	rbi.fStyle		=	RBBS_FIXEDSIZE;   
	rbi.cxMinChild	=	150;      
	rbi.cyMinChild	=	1024;
	rbi.lpText		=	_T("");   
	rbi.cch			=   0;//rect.Height();   
	rbi.cx			=   0;//rect.Width()*0.1;   
	rbi.hwndChild	=   (HWND)(*m_pDialogMain);   
	m_wndReBar.GetReBarCtrl().InsertBand(-1,&rbi);   
	m_pDialogMain->ShowWindow(SW_SHOW);   
	

	return 0;
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
	CCreateContext* pContext)
{
	CRect rect;
	this->GetClientRect(&rect);
	CSize  size;
	size.cx = (rect.Width()-150)*0.5;
	size.cy = rect.Height()*0.5;

	if(!m_wndSplitter.CreateStatic(this, 2,2))
		return false;

	m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CAxialView), size, pContext);
	m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CCoronalView), size, pContext);
	m_wndSplitter.CreateView(1, 0, RUNTIME_CLASS(CSagittalView), size, pContext);
	m_wndSplitter.CreateView(1, 1, RUNTIME_CLASS(CMprView), size, pContext);

	bSplitter = true;

	return true;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
	
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
#ifdef TEST
	CRect rect;
	this->GetClientRect(&rect);
	
	if(bSplitter&&nType!=SIZE_MINIMIZED)
	{
		int cxCur0, cxCur1, cyCur0,  cyCur1, cxMin;
		m_wndSplitter.GetColumnInfo(0, cxCur0, cxMin);
		m_wndSplitter.GetColumnInfo(1, cxCur1, cxMin);
		m_wndSplitter.GetRowInfo(0, cyCur0, cxMin);
		m_wndSplitter.GetRowInfo(1, cyCur1, cxMin);

		cxCur1 = cxCur1 + 10;
		float fColRatio = (float)cxCur0/(cxCur0+cxCur1);

		int x = (rect.Width()-150)*fColRatio, y = (rect.Width()-150)*(1-fColRatio);
		m_wndSplitter.SetColumnInfo(0, (rect.Width()-150)*fColRatio, cxMin);
		m_wndSplitter.SetColumnInfo(1, (rect.Width()-150)*(1-fColRatio), cxMin);
		
		cyCur1 = cyCur1 + 10;
		float fRowRatio = (float)cyCur0/(cyCur0+cyCur1);
		
		int x1 = rect.Height()*fRowRatio, y1 = rect.Height()*(1-fRowRatio);
		m_wndSplitter.SetRowInfo(0, rect.Height()*fRowRatio, cxMin);
		m_wndSplitter.SetRowInfo(1, rect.Height()*(1-fRowRatio), cxMin);
		
		m_wndSplitter.RecalcLayout();
	}
	else{}
#endif
	CFrameWnd::OnSize(nType, cx, cy);

}

CWnd* CMainFrame::GetSplitterWnd(void)
{
	return &m_wndSplitter;
}


// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG


// CMainFrame message handlers

BOOL CMainFrame::OnEraseBkgnd(CDC*pDC)
{
	return true;
}


